package cn.yunyichina.provider.base.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yunyichina.provider.base.dao.PlatformDeptDao;
import cn.yunyichina.provider.base.entity.PlatformDept;
import cn.yunyichina.provider.base.utils.StringUtils;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.dubbo.service.CacheService;
import cn.yunyichina.provider.iface.entity.cache.SessionInfo;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;

/**
 * 
 * @projectName:yyt-provider-base
 * @author:yangfan
 * @description:平台科室基础操作类
 * @date:2016年11月7日
 * @className:PlatFormDeptServiceImpl
 * @pakeage:cn.yunyichina.provider.base.service.impl
 */
@Service
public class PlatformdeptServiceImpl {
	@Autowired
	private PlatformDeptDao platformDeptDao;
	
	
	private static final Logger logger = LoggerFactory.getLogger(PlatformdeptServiceImpl.class);

	/**
	 * 
	 * @methodName:insertPlatformdept
	 * @description:新增平台科室
	 * @author:yangfan
	 * @date:2016年11月7日
	 * @param data 科室信息
	 * @param sessionId 用户缓存id
	 * @return:Response
	 */
	public Response insertPlatformdept(String data, String sessionId) {
		Response response = new Response();
		PlatformDept pl = JsonUtils.parseObject(data, PlatformDept.class);
		if (null == pl) {
			response.setResultCodeAndMessage("1101", "科室信息不能为空");
			return response;
		}
		if (StringUtils.isBlank(pl.getDeptName())) {
			response.setResultCodeAndMessage("1101", "科室名称不能为空");
			return response;
		}
		if (StringUtils.isBlank(pl.getDeptCode())) {
			response.setResultCodeAndMessage("1101", "科室代码不能为空");
			return response;
		}
		if (StringUtils.isBlank(pl.getParentId())) {
			response.setResultCodeAndMessage("1101", "父级科室代码不能为空");
			return response;
		}
		if (null == pl.getShowOrders()) {
			response.setResultCodeAndMessage("1101", "排序编号不能为空");
			return response;
		}
		/* 验证科室代码是否重复添加 */
		PlatformDept plSelect = new PlatformDept();
		plSelect.setDeptCode(pl.getDeptCode());
		Long i = null;
		try {
			i = this.platformDeptDao.selectCount(plSelect);
			logger.info("新增平台科室---->查询平台科室成功");
		} catch (Exception e) {
			logger.error("新增平台科室失败,错误信息:" + e.toString(), e.getMessage());
			response.setResultCodeAndMessage("1999", "系统异常，新增科室---->查询平台科室失败");
			return response;
		}
		if (i > 0) {
			response.setResultCodeAndMessage("1003", "科室代码重复");
			return response;
		}
		try {
			SessionInfo sessions = CacheService.getSessionInfo(sessionId);
			if (null == sessions) {
				response.setResultCodeAndMessage("1101", "用户信息为空，请先登录");
				return response;
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pl.setId(PKGenerator.generateId());
			pl.setCreatedBy(sessions.getUserId());
			pl.setEnableFlag(((short) 1));
			pl.setCreatedTime(sdf.format(new Date()));
			platformDeptDao.insert(pl);
			response.setResultCodeAndMessage("0", "新增平台科室成功");
			logger.info("新增平台科室成功,时间：" + sdf.format(new Date()), "操作人：" + sessions.getUserName());
		} catch (Exception e) {
			logger.error("新增平台科室失败,错误信息:" + e.toString(), e.getMessage());
			response.setResultCodeAndMessage("1999", "系统异常，新增平台科室失败");
			return response;
		}
		return response;
	}

	/**
	 * 
	 * @methodName:updatePlatformdept
	 * @description:修改平台公共科室信息
	 * @author:yangfan
	 * @date:2016年11月7日
	 * @param data       科室信息
	 * @param sessionId  用户缓存id
	 * @return:Response
	 */
	public Response updatePlatformdept(String data, String sessionId) {
		Response response = new Response();
		PlatformDept pl = JsonUtils.parseObject(data, PlatformDept.class);
		SessionInfo sessions = CacheService.getSessionInfo(sessionId);
		if (null == sessions) {
			response.setResultCodeAndMessage("1101", "用户信息为空，请先登录");
			return response;
		}
		if(StringUtils.isBlank(pl.getId())){
			response.setResultCodeAndMessage("1101", "科室id不能为空");
			return response;
		}
		/* 验证科室代码是否重复*/
		PlatformDept plSelect = new PlatformDept();
		plSelect.setDeptCode(pl.getDeptCode());
		Long i = null;
		try {
			i = this.platformDeptDao.selectCount(plSelect);
			logger.info("修改平台科室---->查询平台科室成功");
		} catch (Exception e) {
			logger.error("修改平台科室失败,错误信息:" + e.toString(), e.getMessage());
			response.setResultCodeAndMessage("1999", "系统异常，修改科室---->查询平台科室失败");
			return response;
		}
		if (i > 0) {
			response.setResultCodeAndMessage("1003", "科室代码重复");
			return response;
		}

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pl.setUpdateTime(sdf.format(new Date()));
			pl.setBeginUpdateTime(sdf.format(new Date()));
			pl.setEndUpdateTime(sdf.format(new Date()));
			pl.setUpdateBy(sessions.getUserId());
			this.platformDeptDao.update(pl);
			response.setResultCodeAndMessage("0", "修改成功");
			logger.info("修改平台科室成功,时间：" + sdf.format(new Date()), "操作人：" + sessions.getUserName());
		} catch (Exception e) {
			logger.error("修改平台科室失败,错误信息:" + e.toString(), e.getMessage());
			response.setResultCodeAndMessage("1999", "系统异常，修改平台科室失败");
			return response;
		}
		return response;
	}
    /**
     * 
     * @methodName:selectPlatformdept
     * @description:科室信息查询
     * @author:yangfan
     * @date:2016年11月8日
     * @param data 科室信息
     * @return:Response
     */
	public Response selectPlatformdept(String data){
		Response response = new Response();
		PlatformDept pl = new PlatformDept();
		if(!StringUtils.isBlank(data)){
			pl = JsonUtils.parseObject(data, PlatformDept.class);
		}
		pl.setEnableFlag((short)1);
		List<PlatformDept> pllist = this.platformDeptDao.select(pl);
		response.setResult(JsonUtils.toJsonString(pllist));
		response.setResultCodeAndMessage("0", "平台科室查询成功");
		return response;
	}
	
}
